package com.blb.service;

import java.io.IOException;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.blb.entity.Menu;
import com.blb.entity.MenuExample;
import com.blb.entity.Role;
import com.blb.entity.RoleExample;
import com.blb.entity.RoleExample.Criteria;
import com.blb.entity.Rolemenu;
import com.blb.entity.RolemenuExample;
import com.blb.mapper.MenuMapper;
import com.blb.mapper.RoleMapper;
import com.blb.mapper.RolemenuMapper;
import com.blb.utils.LayuiPage;
import com.mysql.jdbc.StringUtils;

@Service
public class RoleService {

	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private MenuMapper menuMapper;
	
	@Autowired
	private RolemenuMapper rolemenuMapper;
	
	/**
	 * 分页
	 * @param page
	 * @param limit
	 * @param rolename
	 * @return
	 * @throws IOException
	 */
	public LayuiPage page(int page,int limit,String rolename) throws IOException{
		List<Role> list = roleMapper.page(page, limit, rolename);
		
		RoleExample roleExample = new RoleExample();
		Criteria criteria = roleExample.createCriteria();
		if(StringUtils.isNullOrEmpty(rolename)){
			criteria.andRolenameLike("%"+ rolename + "%");
		}
		
		int count = (int) roleMapper.countByExample(roleExample);
		
		LayuiPage layuiPage = new LayuiPage();
		layuiPage.setCount(count);
		layuiPage.setData(list);
		
		return layuiPage;
	}
	
	/**
	 * 判断角色是否存在
	 * @param rolename
	 * @return
	 * @throws IOException
	 */
	public List<Role> isExist(String rolename) throws IOException{
		RoleExample roleExample = new RoleExample();
		Criteria criteria = roleExample.createCriteria();
		criteria.andRolenameEqualTo(rolename);
		
		List<Role> list = roleMapper.selectByExample(roleExample);
		return list;
	}
	
	/**
	 * 添加角色
	 * @param role
	 * @throws IOException
	 */
	@Transactional(rollbackFor=Exception.class)
	public String add(Role role,String menu) throws IOException{
		try{
			roleMapper.insertSelective(role);
			
			String[] menus = menu.split(",");
			for(String m : menus){
				Rolemenu rolemenu = new Rolemenu();
				rolemenu.setRoleid(role.getId());
				rolemenu.setMenuid(m);
				rolemenuMapper.insertSelective(rolemenu);
			}
		}catch (Exception e) {
			return e.getMessage();
		}
		return "";
		
	}
	
	/**
	 * 修改角色
	 * @param role
	 * @throws IOException
	 */
	@Transactional(rollbackFor=Exception.class)
	public String update(Role role,String menus) throws IOException{
		try{
			RoleExample roleExample = new RoleExample();
			Criteria criteria = roleExample.createCriteria();
			criteria.andIdEqualTo(role.getId());
			roleMapper.updateByExample(role, roleExample);
			
			//1、删除
			RolemenuExample rolemenuExample = new RolemenuExample();
			rolemenuExample.createCriteria().andRoleidEqualTo(role.getId());
			rolemenuMapper.deleteByExample(rolemenuExample);
			
			//2、添加
			String[] menu = menus.split(",");
			for(String m : menu){
				Rolemenu rolemenu = new Rolemenu();
				rolemenu.setRoleid(role.getId());
				rolemenu.setMenuid(m);
				rolemenuMapper.insertSelective(rolemenu);
			}
		}catch (Exception e) {
			return e.getMessage();
		}
		
		return "";
		
	}
	
	
	/**
	 * 删除角色
	 * @param roleIds
	 * @throws IOException
	 */
	@Transactional(rollbackFor=Exception.class)
	public String delete(List<String> roleIds) throws IOException{
		try{
			
			RoleExample roleExample = new RoleExample();
			Criteria criteria = roleExample.createCriteria();
			criteria.andIdIn(roleIds);
			roleMapper.deleteByExample(roleExample);
			
			RolemenuExample rolemenuExample = new RolemenuExample();
			rolemenuExample.createCriteria().andRoleidIn(roleIds);
			rolemenuMapper.deleteByExample(rolemenuExample);
			
		}catch (Exception e) {
			return e.getMessage();
		}
		
		return "";
	}
	
	/**
	 * 获取父级菜单
	 * @return
	 * @throws IOException
	 */
	public List<Map<String, Object>> getParentMenu() throws IOException{
		
		MenuExample menuExample = new MenuExample();
		menuExample.createCriteria().andPidEqualTo("0");
		
		//查询所有目录层次的菜单
		List<Menu> list = menuMapper.selectByExample(menuExample);
		
		//menus就是下拉框需要的数据格式
		List<Map<String, Object>> menus = new ArrayList<Map<String, Object>>();
		
		for (Menu m : list) {
			HashMap<String,Object> map = new HashMap<String,Object>();
			
			map.put("name", m.getMenuname());
			map.put("value",m.getId());
			
			//根据当前菜单id查询该菜单的子菜单
			MenuExample menuExample2 = new MenuExample();
			menuExample2.createCriteria().andPidEqualTo(m.getId());
			List<Menu> list2 = menuMapper.selectByExample(menuExample2);
			//将子菜单转化格式
			List<Map<String, Object>> childrens = new ArrayList<Map<String, Object>>();
			for (Menu mm : list2) {
				HashMap<String,Object> map2 = new HashMap<String,Object>();
				map2.put("name", mm.getMenuname());
				map2.put("value",mm.getId());
				
				List<Map<String, Object>> son = new ArrayList<Map<String, Object>>();
				
				//根据当前菜单id查询该菜单的子菜单
				MenuExample menuExample3 = new MenuExample();
				menuExample3.createCriteria().andPidEqualTo(mm.getId());
				List<Menu> list3 = menuMapper.selectByExample(menuExample3);
				for (Menu mmm : list3) {
					HashMap<String,Object> map3 = new HashMap<String,Object>();
					
					map3.put("name", mmm.getMenuname());
					map3.put("value",mmm.getId());
					son.add(map3);
				}
				map2.put("children", son);
				childrens.add(map2);
			}
			map.put("children", childrens);
			menus.add(map);
		}
		return menus;
	}
	
	/**
	 * 根据角色ID获取角色菜单关联表中的对应菜单
	 * @param role
	 * @return
	 * @throws IOException
	 */
	public List<Rolemenu> getMenuByrole(Role role) throws IOException{
		
		
		RolemenuExample rolemenuExample = new RolemenuExample();
		
		rolemenuExample.createCriteria().andRoleidEqualTo(role.getId());
		List<Rolemenu> roleMenuList = rolemenuMapper.selectByExample(rolemenuExample);
		return roleMenuList; 
	}
}
